package com.situ.mall.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.situ.mall.mapper.CategoryMapper;
import com.situ.mall.pojo.Category;
import com.situ.mall.pojo.query.CategoryQuery;
import com.situ.mall.pojo.vo.CategoryVO;
import com.situ.mall.service.ICategoryService;
import com.situ.mall.util.LayUITableResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CategoryServiceImpl implements ICategoryService {
    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public List<Category> selectTopCategory() {
        return categoryMapper.selectTopCategoryList();
    }

    @Override
    public List<Category> selectSecondCategory(Integer parentId) {
        return categoryMapper.selectSecondCategoryById(parentId);
    }

    @Override
    public LayUITableResult selectByPage(CategoryQuery categoryQuery) {
        PageHelper.startPage(categoryQuery.getPage(), categoryQuery.getLimit());
        List<Category> list = categoryMapper.selectByPage(categoryQuery);
        PageInfo pageInfo = new PageInfo(list);
        long total = pageInfo.getTotal();
        return LayUITableResult.ok((int)total, list);
    }

    @Override
    public boolean insert(Category category) {
        return categoryMapper.insertSelective(category) > 0;
    }

    @Override
    public boolean deleteById(Integer id) {
        return categoryMapper.deleteByPrimaryKey(id) > 0;
    }

    @Override
    public boolean deleteAll(Integer[] ids) {
        int count = 0;
        for (Integer id : ids) {
            count += categoryMapper.deleteByPrimaryKey(id);
        }
        return count > 0;
    }

    @Override
    public CategoryVO selectById(Integer id) {
        return categoryMapper.selectVOById(id);
    }

    @Override
    public boolean update(Category category) {
        return categoryMapper.updateByPrimaryKeySelective(category) > 0;
    }
}
